package com.adonis.ejoy.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.adonis.ejoy.Item.Comment;
import com.adonis.ejoy.R;
import com.adonis.ejoy.utils.CircleTransformation;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CommentsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private View view = null;
    private String baseURL;
    private List<Comment> comments;

    public static class CommentViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.ivUserAvatar)
        ImageView ivUserAvatar;
        @BindView(R.id.tvComment)
        TextView tvComment;

        public CommentViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    public CommentsAdapter(List<Comment> comments) {
        this.comments = comments;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_comment, parent, false);
        baseURL = parent.getContext().getString(R.string.baseURL);
        return new CommentViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        CommentViewHolder holder = (CommentViewHolder) viewHolder;
        Comment comment = comments.get(position);
        holder.tvComment.setText(comment.getName() + ": " + comment.getComment());
        Picasso.with(view.getContext())
                .load(baseURL + "img/" +comment.getImg())
                .transform(new CircleTransformation())
                .into(holder.ivUserAvatar);
    }

    @Override
    public int getItemCount() {
        return comments.size();
    }
}
